#include "CollisionSystem.h"

#include <cstdio>

CollisionSystem::CollisionSystem(const AABB& aabb)
{
	octree = new Octree(aabb, 0);
}

void CollisionSystem::addObject(Object *o)
{
	if(!o)
		return;
	o->collisionSystem = this;
	set<Triangle*>::iterator t;
	for(t = o->triangle.begin(); t != o->triangle.end(); ++t) {
		//octree->insert(*t);
		triangle.insert(*t);
	}
}

/*
 * Note: we assume that the object's triangle set didn't change since
 * the object was added.
 */
void CollisionSystem::removeObject(Object *o)
{
	if(!o)
		return;
	set<Triangle*>::iterator t;
	for(t = o->triangle.begin(); t != o->triangle.end(); ++t) {
		//octree->erase(*t);
		triangle.erase(*t);
	}
}

void CollisionSystem::update(Triangle* t)
{
	//octree->update(t);
}

void CollisionSystem::handleCollisions()
{
	//fprintf(stderr, "CollisionSystem::handleCollision\n");
	//octree->handleCollisions();
	return;

	// Compute a set of unique particles
	set<Particle*> particle;
	set<Particle*>::iterator p;
	set<Triangle*>::iterator t;
	for(t = triangle.begin(); t != triangle.end(); ++t) {
		particle.insert((*t)->p[0]);
		particle.insert((*t)->p[1]);
		particle.insert((*t)->p[2]);
	}
	// Collide each triangle with all particles
	for(t = triangle.begin(); t != triangle.end(); ++t) {
		for(p = particle.begin(); p != particle.end(); ++p) {
			(*t)->collide(*p);
		}
	}
}

CollisionSystem::~CollisionSystem()
{
	delete octree;
}
